MatSource.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright 2010-2011 Alessandro Francescon
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "stdafx.h"
  17. #include "MatSource.h"
  18. #include <zxing/common/IllegalArgumentException.h>
  19. zxing::Ref<zxing::LuminanceSource> MatSource::create(cv::Mat & cvImage) {
  20. return zxing::Ref<LuminanceSource>(new MatSource(cvImage));
  21. }
  22. MatSource::MatSource(cv::Mat & _cvImage) : zxing::LuminanceSource(_cvImage.cols, _cvImage.rows) {
  23. cvImage = _cvImage.clone();
  24. }
  25. zxing::ArrayRef<char> MatSource::getRow(int y, zxing::ArrayRef<char> row) const {
  26. // Get width
  27. int width = getWidth();
  28. if (!row) {
  29. // Create row
  30. row = zxing::ArrayRef<char>(width);
  31. }
  32. // Get pointer to row
  33. const char *p = cvImage.ptr<char>(y);
  34. for(int x = 0; x < width; ++x, ++p) {
  35. // Set row at index x
  36. row[x] = *p;
  37. }
  38. return row;
  39. }
  40. zxing::ArrayRef<char> MatSource::getMatrix() const {
  41. // Get width and height
  42. int width = getWidth();
  43. int height = getHeight();
  44. // Create matrix
  45. zxing::ArrayRef<char> matrix = zxing::ArrayRef<char>(width * height);
  46. for (int y = 0; y < height; ++y) {
  47. // Get pointer to row
  48. const char *p = cvImage.ptr<char>(y);
  49. // Calculate y offset
  50. int yoffset = y * width;
  51. for(int x = 0; x < width; ++x, ++p) {
  52. // Set row at index x with y offset
  53. matrix[yoffset + x] = *p;
  54. }
  55. }
  56. return matrix;
  57. }