2012-07-28 4 views
0

사진을 찍은 후 사진의 픽셀 배열에 액세스해야하는 앱을 만들고 있습니다. 주요 활동은 다음과 같습니다. 나는 좋은 양의 자바 경험을 가지고 있지만 화면 상에 과거의 이미지를 표시하는 경험은 극히 제한적이다. 그림 배열 콜백 메서드에 전달되는 바이트 배열을 볼 수 있지만 형식을 알 수 없습니다. 어떻게 캡처 된 이미지의 RGB 컴포넌트를 포함하는 픽셀 배열을 얻을 수 있습니까? 스택 오버플로 포럼을 통해 이것을 찾으려고 노력했지만 몇 백 페이지의 결과를 얻었으며 처음 10 분 정도만 검색 했으므로 미안하지만 이미 질문을 받았지만 방금 보지 못했습니다.카메라로 촬영 한 이미지에서 픽셀 배열을 가져 오는 중

public class ConverterActivity extends Activity 
{ 
    private Camera mCamera; 
    private CameraPreview mPreview; 
private PictureCallback mPicture = new PictureCallback() { 
private String TAG; 
@Override 
public void onPictureTaken(byte[] data, Camera camera) { 
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE); 
if (pictureFile == null){ 
// Log.d(TAG, "Error creating media file, check storage permissions: " + 
// e.getMessage()); 
return; 
} 
try { 
FileOutputStream fos = new FileOutputStream(pictureFile); 
fos.write(data); 
fos.close(); 
} catch (FileNotFoundException e) { 
Log.d(TAG, "File not found: " + e.getMessage()); 
} catch (IOException e) { 
Log.d(TAG, "Error accessing file: " + e.getMessage()); 
} 
} 
}; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

// Add a listener to the Capture button 
Button captureButton = (Button) findViewById(R.id.button_capture); 
captureButton.setOnClickListener(
new View.OnClickListener() { 
@Override 
public void onClick(View v) { 
// get an image from the camera 
mCamera.takePicture(null, null, mPicture); 
} 
} 
); 
     // Create an instance of Camera 
     mCamera = Camera.open(this.getBackCamera()); 
     // Create our Preview view and set it as the content of our activity. 
     mPreview = new CameraPreview(this, mCamera); 
     FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview); 
     preview.addView(mPreview); 
} 
@Override 
    protected void onPause() 
    { 
     super.onPause(); 
     releaseCamera();    // release the camera immediately on pause event 
    } 
    private void releaseCamera(){ 
     if (mCamera != null){ 
      mCamera.release();  // release the camera for other applications 
      mCamera = null; 
     } 
    } 
public static final int MEDIA_TYPE_IMAGE = 1; 
public static final int MEDIA_TYPE_VIDEO = 2; 
/** Create a file Uri for saving an image or video */ 
private static Uri getOutputMediaFileUri(int type){ 
return Uri.fromFile(getOutputMediaFile(type)); 
} 
/** Create a File for saving an image or video */ 
private static File getOutputMediaFile(int type){ 
// To be safe, you should check that the SDCard is mounted 
// using Environment.getExternalStorageState() before doing this. 
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraApp"); 
// This location works best if you want the created images to be shared 
// between applications and persist after your app has been uninstalled. 
// Create the storage directory if it does not exist 
if (! mediaStorageDir.exists()){ 
if (! mediaStorageDir.mkdirs()){ 
Log.d("MyCameraApp", "failed to create directory"); 
return null; 
} 
} 
// Create a media file name 
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
File mediaFile; 
if (type == MEDIA_TYPE_IMAGE){ 
mediaFile = new File(mediaStorageDir.getPath() + File.separator + 
"IMG_"+ timeStamp + ".jpg"); 
} else if(type == MEDIA_TYPE_VIDEO) { 
mediaFile = new File(mediaStorageDir.getPath() + File.separator + 
"VID_"+ timeStamp + ".mp4"); 
} else { 
return null; 
} 
return mediaFile; 
} 
public int getBackCamera() 
{ 
int numCameras = Camera.getNumberOfCameras(); 
CameraInfo cInfo = new CameraInfo(); 
for (int i = 0; i < numCameras; i++) 
{ 
Camera.getCameraInfo(i, cInfo); 
if (cInfo.facing == CameraInfo.CAMERA_FACING_BACK) 
{ 
return i; 
} 
} 
return -1; 
} 
} 

답변

0

이 같은 코드를 사용하여 사진을 촬영하는 경우 :

다음
imgFile = new File(Environment.getExternalStorageDirectory() + "/somefolder/" + name + ".jpg"); 

    String fileName = imgFile.getAbsolutePath(); 

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(fileName))); 

    startActivityForResult(intent, REQUEST_FROM_CAMERA); 

당신이 할 수 있어야, 당신은 비트 맵을 액세스하기 위해 다음과 같은 코드를 사용하여, 다시이 의도의 결과를 얻을 때

if (imgFile.exists()) { 
     String fileName = file.getAbsolutePath(); 
     BitmapFactory.Options opts = new BitmapFactory.Options(); 
     Bitmap bm; 

     opts.inJustDecodeBounds = false; 

     bm = BitmapFactory.decodeFile(fileName, opts); 
     return bm; 
    } 
    else return null; 

그런 다음 당신은 []

012 바이트와 같은 스트리밍 압축 한 후 변환 등 bitmapfactory 도구를 사용할 수 있습니다
ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
    bm.compress(Bitmap.CompressFormat.JPEG, 100, stream); // 100 = max quality 
    byte[] byteArray = stream.toByteArray(); 
+0

감사합니다. 몇 가지 질문이 있지만 안드로이드 및 이미지에 익숙하지 않습니다. 우선이 코드가 대체 무엇입니까? 그냥 사진 찍고 저장하고있어? 또는 카메라 미리보기를 대체합니까? 그리고 바이트 배열은 픽셀을 어떻게 표현합니까? 내가 제공 한 것을 사용하여 특정 픽셀의 RGB 구성 요소를 검색하려면 어떻게해야합니까? –

+0

bitmapfactory를 사용하여 구성 요소 유형을 설정해야합니다. options.inPreferredConfig = Bitmap.Config.RGB_565는 구성 요소 당 2와 3 픽셀로 설정하고 ARGB_8888은 픽셀 당 8 비트로 알파, 빨강, 녹색, 파랑으로 설정합니다. 시도하지는 않았지만, 컴포넌트의 바이트 배열에 인덱스를 넣은 다음 x와 y를 하나의 정수로 계산한다고 생각했습니다. – Martin

+0

대단히 감사합니다. 다른 질문을해야하는 것을 유감스럽게 생각합니다.하지만 마지막 문장에서 무슨 뜻인지 명확하게 설명해 주시겠습니까? "시도하지는 않았지만 구성 요소에서이 바이트 배열로 색인을 생성 한 다음 x 및 y를 단일 정수로 계산한다고 생각합니다."구성 요소의 바이트 배열에 색인을 붙이면 무슨 뜻인지 이해하지 못합니다. "x 및 y가 단일 정수로 계산 됨"을 의미합니다. –

관련 문제